﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using AssetAllocationSystem.Models;
using AssetAllocationSystem.Repositories;
using AssetAllocationSystem.Tests.RepositoriesFixtures;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Tests.Repositories
{
    /// <summary>
    /// Summary description for AllocationRepositoryFixture
    /// </summary>
    [TestClass]
    public class AllocationRepositoryFixture : FixtureBase
    {
        private static ISession session;

        public AllocationRepositoryFixture()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            session = CreateSession();

            for (int i = 1; i <= 5; i++)
            {
                var objAllocation = new Allocation();
                objAllocation.Id = Guid.NewGuid();
                objAllocation.ActualReleaseDate = new DateTime(2012, 1, i*3);
                objAllocation.ExpectedReleaseDate = new DateTime(2012, 1, i * 3);
                
                // Change this to accept bit or integer in database - C# enum
                objAllocation.AllocationType = "leasehold";

                var employee = new Employee();
                employee.Allocations = null;
                employee.Company = null;
                employee.Id = Guid.NewGuid();
                employee.Location = new Location();
                employee.Location.Id = Guid.NewGuid();
                employee.Location.Code = "Location1";
                employee.Name = "Employee1";
                employee.Requests = null;

                var assignee = new Vendor();
                assignee.Id = Guid.NewGuid();
                assignee.Name = "Vendor_Assignee";
                session.Save(assignee);

                objAllocation.Asset = null;
                objAllocation.Assignee = assignee;
                
                session.Save(objAllocation);
            }

            session.Flush();
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            var allocationRepository = new AllocationRepository(session);
            var allocations = allocationRepository.FindAll();

            foreach (var allocation in allocations)
            {
                session.Delete(allocation);
            }

            session.Flush();
        }
        
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() { }
        
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup() { }
        
        #endregion

        [TestMethod, Ignore]
        public void Check_if_FindByAvailability_works_well()
        {
            //var allocationRepository = new AllocationRepository(session);
            //var allocation = allocationRepository.FindByAvailability(true);

            //Assert.IsNotNull(allocation);
            //Assert.AreNotEqual(0, allocation.Count);
        }

        [TestMethod]
        public void Check_if_ExpectedReleaseDate_works_well()
        {
            var allocationRepository = new AllocationRepository(session);
            var allocation = allocationRepository.FindByExpectedReleaseDate(new DateTime(2012, 1, 10));

            Assert.IsNotNull(allocation);
            Assert.AreNotEqual(0, allocation.Count);
        }

        [TestMethod]
        public void Check_if_ActualReleaseDate_works_well()
        {
            var allocationRepository = new AllocationRepository(session);
            var allocation = allocationRepository.FindByActualReleaseDate(new DateTime(2012, 1, 10));

            Assert.IsNotNull(allocation);
            Assert.AreNotEqual(0, allocation.Count);
        }
        
        [TestMethod]
        public void Check_if_FindByAllocationType_works_well()
        {
            var allocationRepository = new AllocationRepository(session);
            var allocation = allocationRepository.FindByAllocationType("leasehold");

            Assert.IsNotNull(allocation);
            Assert.AreNotEqual(0, allocation.Count);
        }
    }
}
